package com.ruoyi.project.business.salaryrule.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.project.business.salaryrule.entity.MonthSalaryRule;
import com.ruoyi.project.business.salaryrule.mapper.MonthSalaryRuleMapper;
import com.ruoyi.project.business.salaryrule.request.AddMonthSalaryRuleRequest;
import com.ruoyi.project.business.salaryrule.request.EditMonthSalaryRuleRequest;
import com.ruoyi.project.business.salaryrule.request.ListMonthSalaryRuleRequest;
import com.ruoyi.project.business.salaryrule.response.MonthSalaryRuleResponse;
import com.ruoyi.project.business.salaryrule.service.IMonthSalaryRuleService;
import com.ruoyi.project.business.utils.BusinessUtil;
import com.ruoyi.project.system.service.ISysDeptService;
import com.ruoyi.project.system.service.ISysUserService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 月薪计算规则Service业务层处理
 * 
 * @author ${author}
 * @date 2024-02-21
 */
@Service
public class MonthSalaryRuleServiceImpl implements IMonthSalaryRuleService {
    @Autowired
    private MonthSalaryRuleMapper monthSalaryRuleMapper;
    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private ISysDeptService sysDeptService;

    /**
     * 查询月薪计算规则
     * 
     * @return 月薪计算规则
     */
    @Override
    public MonthSalaryRule selectMonthSalaryRuleById(Long id) {
        return monthSalaryRuleMapper.selectMonthSalaryRuleById(id);
    }

    /**
     * 查询月薪计算规则列表
     * 
     * @return 月薪计算规则
     */
    @Override
    public List<MonthSalaryRuleResponse> selectMonthSalaryRuleList(ListMonthSalaryRuleRequest request, Long tenantId) {
        // TODO request to  param
        MonthSalaryRule param = BeanUtils.copy(request, MonthSalaryRule.class);
        param.setTenantId(tenantId);
//        Date yearMonth = request.getYearMonth();
//        Integer year = DateUtils.year(yearMonth);
//        Integer month = DateUtils.month(yearMonth);
//        param.setYear(year);
//        param.setMonth(month);
        List<MonthSalaryRule> entitis = monthSalaryRuleMapper.selectMonthSalaryRuleList(param);
        List<MonthSalaryRuleResponse> resList = BeanUtils.copyList(entitis, MonthSalaryRuleResponse.class);

        BusinessUtil.fillListNames(resList, tenantId);
        return resList;
    }

    /**
     * 新增月薪计算规则
     * 
     * @return 结果
     */
    @Override
    public int insertMonthSalaryRule(AddMonthSalaryRuleRequest request, Long deptId, Long operatorId, Long tenantId) {
        MonthSalaryRule entity = BeanUtils.copy(request, MonthSalaryRule.class);
        Date yearMonth = request.getYearMonth();
        Integer year = DateUtils.year(yearMonth);
        Integer month = DateUtils.month(yearMonth);
        entity.setDeptId(deptId);
        entity.setYear(year);
        entity.setMonth(month);
        entity.setCreateTime(DateUtils.getNowDate());
        entity.setTenantId(tenantId);
        entity.setCreator(operatorId);
        entity.setDeleted(Boolean.FALSE);
        entity.setCreateTime(DateUtils.getNowDate());
        return monthSalaryRuleMapper.insertMonthSalaryRule(entity);
    }

    /**
     * 修改月薪计算规则
     * 
     * @return 结果
     */
    @Override
    public int updateMonthSalaryRule(EditMonthSalaryRuleRequest request, Long operatorId, Long tenantId) {
        MonthSalaryRule monthSalaryRule = BeanUtils.copy(request, MonthSalaryRule.class);

        Date yearMonth = request.getYearMonth();
        Integer year = DateUtils.year(yearMonth);
        Integer month = DateUtils.month(yearMonth);

        monthSalaryRule.setYear(year);
        monthSalaryRule.setMonth(month);
        monthSalaryRule.setUpdateTime(DateUtils.getNowDate());
        monthSalaryRule.setTenantId(tenantId);
        monthSalaryRule.setUpdater(operatorId);
        return monthSalaryRuleMapper.updateMonthSalaryRule(monthSalaryRule);
    }

    /**
     * 批量删除月薪计算规则
     * 
     * @return 结果
     */
    @Override
    public int deleteMonthSalaryRuleByIds(Long[] ids, Long operatorId, Long tenantId) {
        return monthSalaryRuleMapper.deleteMonthSalaryRuleByIds(ids);
    }

    /**
     * 删除月薪计算规则信息
     * 
     * @return 结果
     */
    @Override
    public int deleteMonthSalaryRuleById(Long id, Long operatorId, Long tenantId) {
        return monthSalaryRuleMapper.deleteMonthSalaryRuleById(id);
    }

    @Override
    public MonthSalaryRule selectRuleByDeptId(Long deptId, Long tenantId) {
        MonthSalaryRule param = new MonthSalaryRule();
        param.setDeptId(deptId);
        param.setTenantId(tenantId);
        List<MonthSalaryRule> entitis = monthSalaryRuleMapper.selectMonthSalaryRuleList(param);
        if(CollectionUtils.isNotEmpty(entitis)) {
            return entitis.get(0);
        }
        return null;
    }

    @Override
    public MonthSalaryRule selectRuleByUserId(Long userId, Long tenantId) {
        MonthSalaryRule param = new MonthSalaryRule();
        param.setUserId(userId);
        param.setTenantId(tenantId);
        List<MonthSalaryRule> entitis = monthSalaryRuleMapper.selectMonthSalaryRuleList(param);
        if(CollectionUtils.isNotEmpty(entitis)) {
            return entitis.get(0);
        }
        return null;
    }

    @Override
    public Map<Long, MonthSalaryRule> selectRuleByUserIds(List<Long> userIds, Long tenantId) {
        List<MonthSalaryRule> list = monthSalaryRuleMapper.selectRuleByUserIds(userIds, tenantId);
        if(CollectionUtils.isEmpty(list)) {
            return new HashMap<>();
        }
        Map<Long, MonthSalaryRule> collect = list.stream().collect(Collectors.toMap(MonthSalaryRule::getUserId, Function.identity(), (t1, t2) -> t1));
        return collect;
    }
}
